package com.mentalhealthplatform.teacherV2.service;

import com.mentalhealthplatform.appModels.Student;
import com.mentalhealthplatform.teacherV2.dto.StudentDTO;
import com.mentalhealthplatform.teacherV2.repository.StudentRepositoryV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class StudentFamilyServiceV2 {

    @Autowired
    private StudentRepositoryV2 studentRepositoryV2;

    // 查询所有学生信息并转换为学生DTO
    public List<StudentDTO> findAll() {
        return studentRepositoryV2.findAll().stream()
                .map(StudentDTO::convertToDTO)
                .collect(Collectors.toList());
    }

    // 根据ID查询学生信息
    public Optional<Student> findById(UUID id) {
        return studentRepositoryV2.findById(id);
    }

    // 保存学生信息
    public Student saveStudent(Student student) {
        return studentRepositoryV2.save(student);
    }

    // 删除学生信息
    public void deleteById(UUID id) {
        studentRepositoryV2.deleteById(id);
    }

    // 根据关键词搜索学生信息
    public List<StudentDTO> searchByUsernameOrNumber(String keyword) {
        List<Student> students = studentRepositoryV2.findByNameContainingOrNumberContaining(keyword, keyword);
        return students.stream()
                .map(StudentDTO::convertToDTO)
                .collect(Collectors.toList());
    }

    // 根据多个条件查询学生信息
    public List<StudentDTO> searchByMultipleCriteria(String name, String gender, String major, String grade, String college) {
        List<Student> students = studentRepositoryV2.findByNameContainingAndGenderContainingAndMajorContainingAndGradeContainingAndCollegeContaining(
                name, gender, major, grade, college);
        return students.stream()
                .map(StudentDTO::convertToDTO)
                .collect(Collectors.toList());
    }

    // 保存学生信息并更新家庭信息
    public StudentDTO saveStudentAndUpdateFamilyMessages(Student student) {
        // 保存学生信息
        Student savedStudent = studentRepositoryV2.save(student);
        // 更新家长信息的逻辑（如果有）
        // 这里可以添加更新家长信息的代码
        return StudentDTO.convertToDTO(savedStudent);
    }
}